﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Query.DishOrderModule.ViewObject;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.DomainModel;
using Resto.CostControl.Core.Enums;
using Resto.CostControl.Core.Domain.Query.SiteModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Query.DishOrderModule.QueryObject
{
    public class WeekDishOrderQO
    {
        private IQueryable<WeekDishOrderVO> GetWeekDishOrderVOSet()
        {
            IEntityRepository<WeekDishOrder> repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<WeekDishOrder>();

            IQueryable<WeekDishOrderVO> list = from o in repository.Objects
                                               select new WeekDishOrderVO()
                                               {
                                                   ID = o.ID,
                                                   BeginDate = o.BeginDate,
                                                   EndDate = o.EndDate,
                                                   OrderState = o.OrderState,
                                                   SiteId = o.SiteID,
                                               };

            return list;
        }

        //public WeekDishOrderVO[] QueryWeekDishOrderVOByPager(ISpecification<WeekDishOrderVO> spec, PagerCondition pager, string orderKey, bool isAscending)
        //{
        //    IQueryable<WeekDishOrderVO> list = GetWeekDishOrderVOSet();

        //    if (spec != null)
        //        list = list.Where(spec.GetExpression());

        //    IEnumerable<WeekDishOrderVO> list2 = RepositoryExtention.Query(list, pager, orderKey, isAscending);
        //    return list2.ToArray();
        //}

        public WeekDishOrderVO[] QueryNextWeekDishOrderVO()
        {
            IQueryable<WeekDishOrderVO> list = GetWeekDishOrderVOSet();

            DateTime time = DateTime.Now.AddDays(7);
            string state = DishOrderState.Publish.ToString();
               
            list = from o in list
                   where o.BeginDate <= time
                    && o.EndDate >= time
                    && o.OrderState == state
                   select o;

            //已上报的下周菜单
            List<WeekDishOrderVO> result = list.ToList();

            //已上报的项目点
            string[] publishIDs = (from o in result
                                select o.SiteId).ToArray();            

            //所有项目点
            SiteDQO siteDQO = ObjectCreator.Create<SiteDQO>();
            string[] allIDs = siteDQO.GetAllSiteId();

            //未上报的项目点
            string[] ids = (from o in allIDs
                         where !publishIDs.Contains(o)
                         select o).ToArray();

            //构建未上报的项目点
            foreach (string id in ids)
            {
                WeekDishOrderVO order = new WeekDishOrderVO()
                {
                    SiteId = id,
                    BeginDate = TimeHelper.GetNextMonday(),
                    EndDate = TimeHelper.GetNextMonday().AddDays(7),
                    OrderState = DishOrderState.None.ToString(),
                };

                result.Add(order);
            }

            return result.OrderBy(p=>p.SiteName).ToArray();
        }
    }
}
